package com.midea.ai.air.ui.more.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.more.MoreSettingBrainpowerStartCondition_StartTime;
import com.midea.ai.air.ui.more.data.MoreLoggingItem;
import com.midea.ai.air.ui.more.data.MoreSelectedData;

import java.util.List;

/**
 * Created by ouyangdi on 2016/4/11.
 * 选择日期(星期几) 的Adapter
 */
public class MoreSelectedDataAdapter extends ArrayAdapter<MoreSelectedData> {
    private LayoutInflater mInflater;

    public MoreSelectedDataAdapter(Context context, List<MoreSelectedData> objects) {
        super(context, 0, objects);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final MoreSelectedData item = getItem(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_select_data, parent, false);
            holder = new ViewHolder();
            holder.tvContent = (TextView) convertView.findViewById(R.id.selected_data_week);
            holder.Img = (ImageView) convertView.findViewById(R.id.selected_data_iv_selected);
            holder.rl = (RelativeLayout) convertView.findViewById(R.id.selected_data_relativeLayout);
            holder.line = (View) convertView.findViewById(R.id.selected_data_line);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final boolean isSelected = item.ismSelected();
        holder.tvContent.setText(item.getmContent());
        if (isSelected) {
            holder.Img.setVisibility(View.VISIBLE);
        } else {
            holder.Img.setVisibility(View.GONE);
        }

        if (position == getCount() - 1) {
            holder.line.setVisibility(View.GONE);
        } else {
            holder.line.setVisibility(View.VISIBLE);
        }

        holder.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isSelected) {
                    Log.i("hwwlog", "item  结果：" + item.ismSelected());
                    holder.Img.setVisibility(View.GONE);
                    MoreSettingBrainpowerStartCondition_StartTime.mList_Selected.set(position, false);
                    item.setmSelected(false);
                    notifyDataSetChanged();
                } else {
                    holder.Img.setVisibility(View.VISIBLE);
                    MoreSettingBrainpowerStartCondition_StartTime.mList_Selected.set(position, true);
                    item.setmSelected(true);
                    notifyDataSetChanged();
                }
            }
        });

        return convertView;
    }

    public class ViewHolder {
        public TextView tvContent;
        public ImageView Img;
        public RelativeLayout rl;
        public View line;
    }


}
